# -*- coding: utf-8 -*-
import scrapy
import urlparse
from scrapy import Request
from amazon.items import *
from scrapy.selector import Selector

class CategorySpider(scrapy.Spider):
    name = "category"
    url = "https://www.amazon.cn"

    def start_requests(self):
        with open("category.htm") as f:
            content = f.read()
            sel = Selector(text=content)
            for a in sel.css(".sd-fontSizeL2 > a"):
                category = clear_text(get_css_node_text(a.css("::text")))
                url = get_css_node_text(a.css("::attr(href)"))
                yield Request(url, meta={"category": category})


    def parse(self, response):
        category = response.meta["category"]
        li = response.css("#ref_125596071 > li:nth-last-child(0)")
        if li:
            if li.css("span.seeMore"):
                url_cagory = (urlparse.urljoin(self.url, get_css_node_text(li.css("a::attr(href)"))))
                yield Request(url_cagory, callback=self.parse_category, meta={"category": category})
            else:
                for li in response.css("#ref_125596071 > li"):
                    item = AmazonItem()
                    item["category"] = category
                    item["brand"] = clear_text(get_css_node_text(li.css("a > span.refinementLink::text")))
                    item["url"] = response.url
                    yield item

    def parse_category(self, response):
        category = response.meta["category"]
        for brand in response.css(".refinementLink"):
            item = AmazonItem()
            item["category"] = category
            item["brand"] = clear_text(get_css_node_text(brand.css("::text")))
            item["url"] = response.url
            yield item

def get_css_node_text(node):
    if node:
        return node.extract()[0]
    return ""

def clear_text(prop):
    if prop:
        return prop.strip().replace("\r\n", "").replace("\r", "").replace("\n", "").replace(",", u"，")
    return ""